Imports System.Windows.Forms
Imports System.Drawing.Printing

Public Class dlgDetailReport
    Public EmployeeID As String
    Private WithEvents pdPrint As PrintDocument
    Private _typePrint As String = ""
    Public _printerName As String = getOptions("Printer")
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Report.StartDate = New Date(dtpFrom.Value.Year, dtpFrom.Value.Month, dtpFrom.Value.Day, 0, 0, 0)
        Report.EndDate = New Date(dtpTo.Value.Year, dtpTo.Value.Month, dtpTo.Value.Day, 23, 59, 59)

        'Report.couponlimit = nupCoupon.Value
        'Me.DialogResult = System.Windows.Forms.DialogResult.OK
        'Me.Close()
        Report.RType = "DETAIL"
        Report.EmployeeID = EmployeeID
        Report.ShowDialog()


    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


    Private Sub dlgDetailReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtpFrom.Value = New Date(Year(Now()), Month(Now()), 1)
        dtpTo.Value = DateAdd(DateInterval.Day, -1, New Date(Year(DateAdd(DateInterval.Month, 1, Now())), Month(DateAdd(DateInterval.Month, 1, Now())), 1))

        'ChinhLH correct
        'dtpTo.Value = DateAdd(DateInterval.Day, -1, New Date(Year(Now()), Month(Now()) + 1, 1))
        dtpTo.Value = DateAdd(DateInterval.Day, -1, New Date(Year(DateAdd(DateInterval.Month, 1, Now())), Month(DateAdd(DateInterval.Month, 1, Now())), 1))
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

        '---------------- Print report ---------------------
        Try
            _typePrint = "PrintReport"
            pdPrint = New PrintDocument

            pdPrint.PrinterSettings.PrinterName = _printerName

            If pdPrint.PrinterSettings.IsValid Then
                pdPrint.DocumentName = "PrintDocument"
                ' Start printing
                pdPrint.Print()
            Else
                MessageBox.Show("Printer is not available.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Catch ex As Exception

        End Try
        '---------------- End Print report -----------------
    End Sub

    ' The event handler function when pdPrint.Print is called.
    ' This is where the actual printing of sample data to the printer is made.
    Private Sub pdPrint_Print(ByVal sender As System.Object, ByVal e As PrintPageEventArgs) Handles pdPrint.PrintPage

        If _typePrint = "PrintReport" Then

            Dim dt As DataTable
            Dim _Header As String = getOptions("header")

            dt = db.getDetailReport(New Date(dtpFrom.Value.Year, dtpFrom.Value.Month, dtpFrom.Value.Day, 0, 0, 0), New Date(dtpTo.Value.Year, dtpTo.Value.Month, dtpTo.Value.Day, 23, 59, 59), EmployeeID).Tables(0)
            Dim printFont As New Font("FontA11", 10, FontStyle.Regular, GraphicsUnit.Point) ' Substituted to FontA Font
            e.Graphics.PageUnit = GraphicsUnit.Point
            'f.WriteString(_Header & Chr(13) & Chr(10), 0, 1, 1, _Color)
            e.Graphics.DrawString(_Header & Chr(13) & Chr(10), printFont, Brushes.Black, 6, 0)
            'f.WriteString("Name: " & dt.Rows(0).Item("eFullName"), 1, 0, 0, _Color)
            e.Graphics.DrawString("Name: " & dt.Rows(0).Item("eFullName"), printFont, Brushes.Black, 6, 12)
            'f.WriteString("Dept: " & dt.Rows(0).Item("Dep_Name") & Chr(13) & Chr(10), 1, 0, 0, _Color)
            e.Graphics.DrawString("Dept: " & dt.Rows(0).Item("Dep_Name") & Chr(13) & Chr(10), printFont, Brushes.Black, 6, 24)
            'f.WriteString("        Date            Coupon", 1, 1, 0, _Color)
            e.Graphics.DrawString("        Date            Coupon", printFont, Brushes.Black, 6, 36)
            Dim pace As Integer = 48
            For i As Integer = 0 To dt.Rows.Count - 1
                pace += 12
                ' f.WriteString(Format(dt.Rows(i).Item("tDate"), "dd-MMM-yyyy hh:mm:ss") & "    " & dt.Rows(i).Item("coupon"), 1, 0, 0, FrTransactions._Color)
                e.Graphics.DrawString(Format(dt.Rows(i).Item("tDate"), "dd-MMM-yyyy hh:mm:ss") & "    " & dt.Rows(i).Item("coupon"), printFont, Brushes.Black, 6, pace)
            Next
            'f.WriteString("               Total     " & dt.Rows(0).Item("total"), 1, 1, 0, FrTransactions._Color)
            pace += 12
            e.Graphics.DrawString("               Total     " & dt.Rows(0).Item("total"), printFont, Brushes.Black, 6, pace)
            'f.closePrinter()

            'Try
            '    'Dim f As New POSPrinter.U220POS
            '    'Dim printFont As New Font("Microsoft Sans Serif", 10, FontStyle.Regular, GraphicsUnit.Point) ' Substituted to FontA Font
            '    'f.openPrinter(_Port) 'db.getvalue("select * from options where oname = 'Port'", "ovalue")
            '    'f.WriteString(_Header & Chr(13) & Chr(10), 0, 1, 1, _Color)
            '    e.Graphics.DrawString(_Header & Chr(13) & Chr(10), printFont, Brushes.Black, 6, 0)
            '    'f.WriteString("Ordinal: " & row.Cells("OrdinalNumber").Value & IIf(Len(reprint) > 0, " (Reprint:" & reprint & ")", ""), 1, 0, 0, _Color)
            '    e.Graphics.DrawString("Ordinal: " & row.Cells("OrdinalNumber").Value & IIf(Len(reprint) > 0, " (Reprint:" & reprint & ")", ""), printFont, Brushes.Black, 6, 12)
            '    'f.WriteString("Date   : " & Format(Now(), "MMM dd yyyy, hh:mm tt"), 1, 0, 0, _Color)
            '    e.Graphics.DrawString("Date   : " & Format(Now(), "MMM dd yyyy, hh:mm tt"), printFont, Brushes.Black, 6, 24)
            '    'f.WriteString("Name   : " & row.Cells("FullName").Value, 1, 0, 0, _Color)
            '    e.Graphics.DrawString("Name   : " & row.Cells("FullName").Value, printFont, Brushes.Black, 6, 36)
            '    'f.WriteString("Dept   : " & db.getvalue("select * from department where departmentid=(select edepartment from employee where employeeid='" & row.Cells("EmployeeID").Value & "')", "dep_name"), 1, 0, 0, _Color)
            '    e.Graphics.DrawString("Dept   : " & db.getvalue("select * from department_CuChi where departmentid=(select edepartment from  Employee_CuChi  where employeeid='" & row.Cells("EmployeeID").Value & "')", "dep_name"), printFont, Brushes.Black, 6, 48)
            '    'f.WriteString("Card   : " & db.getvalue("select * from employee where employeeID = '" & row.Cells("EmployeeID").Value & "'", "eCardNumber"), 1, 0, 0, _Color)
            '    e.Graphics.DrawString("Card   : " & db.getvalue("select * from employee_CuChi where employeeID = '" & row.Cells("EmployeeID").Value & "'", "eCardNumber"), printFont, Brushes.Black, 6, 60)
            '    'f.WriteString("Coupon : " & db.getvalue("select * from coupon where couponid = '" & row.Cells("Coupon").Value & "'", "Cname") & " (" & row.Cells("Quantity").Value & ")", 1, 0, 0, _Color)
            '    e.Graphics.DrawString("Coupon : " & db.getvalue("select * from Coupon_CuChi where couponid = '" & row.Cells("Coupon").Value & "'", "Cname") & " (" & row.Cells("Quantity").Value & ")", printFont, Brushes.Black, 6, 72)

            '    'f.WriteString("Quantity: " & row.Cells("Quantity").Value, 1, 0, 0, _Color)
            '    If _boolStatus Then
            '        'f.WriteString("Left   : " & LeftC(row.Cells("EmployeeID").Value, row.Cells("Coupon").Value, row.Cells("Quantity").Value), 1, 0, 0, _Color)
            '        e.Graphics.DrawString("Left   : " & LeftC(row.Cells("EmployeeID").Value, row.Cells("Coupon").Value, row.Cells("Quantity").Value), printFont, Brushes.Black, 6, 84)
            '    Else
            '        'f.WriteString("Left   : " & LeftC(row.Cells("EmployeeID").Value, row.Cells("Coupon").Value, 0), 1, 0, 0, _Color)
            '        e.Graphics.DrawString("Left   : " & LeftC(row.Cells("EmployeeID").Value, row.Cells("Coupon").Value, 0), printFont, Brushes.Black, 6, 84)
            '    End If

            '    'f.WriteString(_Footer, 0, 0, 1, _Color)
            '    e.Graphics.DrawString(_Footer, printFont, Brushes.Black, 6, 96)

            '    'f.closePrinter()
            '    e.HasMorePages = False

            '    reprint = ""
            '    'Return True
            'Catch ex As Exception
            '    reprint = ""
            '    e.HasMorePages = False
            '    'Return False
            'End Try
        End If
    End Sub

    Private Sub print()

        Dim print_document As New PrintDocument
        print_document.PrinterSettings = FrMain.dlgPrint.PrinterSettings
        print_document.Print()
    End Sub
End Class
